package com.qyhl.webtv.module_live.utils.ItemViewDelegete;

import android.content.Context;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.qyhl.webtv.basiclib.utils.DateUtils;
import com.qyhl.webtv.basiclib.utils.StringUtils;
import com.qyhl.webtv.basiclib.utils.glide.GlideRoundTransform;
import com.qyhl.webtv.commonlib.entity.Image;
import com.qyhl.webtv.commonlib.entity.live.TeleTextLiveBean;
import com.qyhl.webtv.commonlib.utils.view.NineGridLayout.NineAdapter;
import com.qyhl.webtv.commonlib.utils.view.NineGridLayout.NineLayout;
import com.qyhl.webtv.commonlib.utils.view.expandableView.text.ExpandableTextView;
import com.qyhl.webtv.commonlib.utils.view.imagebrowser.MNImageBrowser;
import com.qyhl.webtv.module_live.R;
import com.zhy.adapter.recyclerview.base.ItemViewDelegate;
import com.zhy.adapter.recyclerview.base.ViewHolder;

import java.util.ArrayList;
import java.util.List;


/**
 * Created by qyhl on 2017/8/14.
 * 图文多图样式
 */

public class MoreIconItemDelegete implements ItemViewDelegate<TeleTextLiveBean> {
    private Context context;

    List<TeleTextLiveBean> liveBeen;
    public MoreIconItemDelegete(Context context,List<TeleTextLiveBean> liveBeen) {
        this.context = context;
        this.liveBeen=liveBeen;
    }

    @Override
    public int getItemViewLayoutId() {
        return R.layout.live_item_teletext_live_icon;
    }

    @Override
    public boolean isForViewType(TeleTextLiveBean item, int position) {
        if ("0".equalsIgnoreCase(item.getType())) {
            if (item.getImages().size() != 1) {
                return true;
            } else {
                return false;
            }
        } else {
            return false;
        }


    }

    @Override
    public void convert(ViewHolder holder, TeleTextLiveBean teleTextLiveBean, int position) {
        //设置时间轴
        TextView time = holder.getView(R.id.item_time);
        time.setText(DateUtils.teleTextDateFormat(teleTextLiveBean.getPublishtime()));//时间

        //设置作者
        TextView author = holder.getView(R.id.item_author);
        if (StringUtils.isEmpty(teleTextLiveBean.getAuthor())) {
            author.setVisibility(View.GONE);
        } else {
            author.setVisibility(View.VISIBLE);
            author.setText(StringUtils.encodePhoneNum(teleTextLiveBean.getAuthor()));
        }

        //设置简介
        ExpandableTextView content = holder.getView(R.id.item_content);
        if (StringUtils.isEmpty(teleTextLiveBean.getContent())) {
            content.setVisibility(View.GONE);
        } else {
            content.setVisibility(View.VISIBLE);
            content.setText(teleTextLiveBean.getContent(),position);
        }

        //设置日期
        TextView date = holder.getView(R.id.item_date);
        LinearLayout dateLayout = holder.getView(R.id.top_layout);
        if (position != 0) {
            if (liveBeen.size() > 0) {
                if (DateUtils.isSameDay(teleTextLiveBean.getPublishtime(), liveBeen.get(position - 1).getPublishtime())) {
                    dateLayout.setVisibility(View.GONE);
                } else {
                    dateLayout.setVisibility(View.VISIBLE);
                    date.setText(DateUtils.teleTextFormat(teleTextLiveBean.getPublishtime()));
                }
            } else {
                dateLayout.setVisibility(View.GONE);
            }
        } else {
            dateLayout.setVisibility(View.VISIBLE);
            date.setText(DateUtils.teleTextFormat(teleTextLiveBean.getPublishtime()));
        }

        NineLayout nineLayout = holder.getView(R.id.nine_layout);
        nineLayout.setGap(10);
        List<Image> imageList = initDatas(teleTextLiveBean.getImages());
        Adapter adapter = new Adapter(context, imageList);
        nineLayout.setAdapter(adapter);
        nineLayout.setTag(R.id.live_nineLayout,teleTextLiveBean);
        nineLayout.setOnItemClickListerner(new NineLayout.OnItemClickListerner() {
            @Override
            public void onItemClick(View view, int position) {
                TeleTextLiveBean telBean = (TeleTextLiveBean) view.getTag(R.id.live_nineLayout);
                ArrayList<String> mList = new ArrayList<>();
                for (int i = 0; i < telBean.getImages().size(); i++) {
                    mList.add(telBean.getImages().get(i).getUrl());
                }
                MNImageBrowser.showImageBrowser(context, view, position, mList);
            }
        });
    }

    //生成九宫格模型
    private List<Image> initDatas(List<TeleTextLiveBean.Images> beanList) {
        List<Image> images = new ArrayList<>();
        for (int i = 0; i < beanList.size(); i++) {
            images.add(new Image(beanList.get(i).getThumbnailUrl(), 0, 0));
        }
        return images;
    }

    class Adapter extends NineAdapter {

        public Adapter(Context context, List<Image> list) {
            super(context, list);
        }

        @Override
        public int getCount() {
            return (list == null) ? 0 : list.size();
        }

        @Override
        public String getUrl(int position) {
            return getItem(position) == null ? null : ((Image) getItem(position)).getUrl();
        }

        @Override
        public Object getItem(int position) {
            return (list == null) ? null : list.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int i, View view) {
            ImageView iv = new ImageView(context);
            Glide.with(context)
                    .load(list.get(i).getUrl())
                    .apply(new RequestOptions().error(R.drawable.cover_normal_default).placeholder(R.drawable.cover_normal_default).centerCrop().optionalTransform(new GlideRoundTransform(4)))
                    .into(iv);
            return iv;
        }
    }
}
